
#include "Cube.h"


Cube::Cube(void)
{
	static const GLfloat g_vertex_buffer_data[] = { 
		-1.0f, -1.0f, -1.0f,
		+1.0f, -1.0f, -1.0f,
		+1.0f, +1.0f, -1.0f,
		-1.0f, +1.0f, -1.0f,

		-1.0f, -1.0f, +1.0f,
		+1.0f, -1.0f, +1.0f,
		+1.0f, +1.0f, +1.0f,
		-1.0f, +1.0f, +1.0f,
	};

	//glGenBuffers(1, &mVertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, mVertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

	// 6 faces * 2 tri by face = 12 tri * 3 vertice = 36 points
	unsigned int Indices[] = {
		0,1,2, 2,0,3,
		3,0,4, 4,3,7,
		7,3,2, 2,7,6,
		6,2,1, 1,6,5, 
		5,1,0, 0,5,4,
		4,7,6, 6,4,5
	};

		glGenBuffers(1, &mIndexedBufferObject);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexedBufferObject);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}


Cube::~Cube(void)
{
}

void Cube::Draw()
{
	// 1rst attribute buffer : vertices
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, mVertexbuffer);
	glVertexAttribPointer(
		0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
		3,                  // size
		GL_FLOAT,           // type
		GL_FALSE,           // normalized?
		0,                  // stride
		(void*)0            // array buffer offset
		);

	// Draw the triangle !
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexedBufferObject);
	glDrawElements(	GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); // 6 faces * 2 tri by face = 12 tri * 3 vertice = 36 points

	glDisableVertexAttribArray(0);
}
